package project.active;

import java.sql.SQLException;
import java.util.ArrayList;

import model.pojo.Employee;
import project.adapter.ProjectCommentErrorAdapter;
import project.adapter.Projectdetail_error_adapter_tc;
import project.dao.CommentDAO;
import project.dao.TestProjectDAO;
import project.model.PrComment;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import approve.action.R;

public class ProcessButtonViewErrorManagerProject extends Activity{
	private String lang="EN",database="admin";
	private Employee emp = new Employee();
	private PrProject prProject  = null;
	private PrTestProject prTestProject = null;
	private TextView title,tester,testDate,status,content,curNameProject;
	private TextView lbTitle,lbTester,lbTestDate,lbStatus,lbContent,listComent;
	private Button buttonComment,buttonFixComplete;
	private ListView lstViewComment;
	private int statuslst = 0;
	TextView logout,username;
	Bundle language;
	String namecolor="#B023AB";//mau tim
	private PrComment prcom;
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	public String getLang(){
    	Bundle extras = getIntent().getExtras();
    	if(extras != null){
    		lang = extras.getString("lang");
    	}
    	return lang;
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buttonviewprojectmanagereror);
		language = getIntent().getExtras().getBundle(getLang());
		curNameProject = (TextView)findViewById(R.id.currentprojectname);
		title = (TextView)findViewById(R.id.valTitle);
		tester  = (TextView)findViewById(R.id.valTester);
		testDate  = (TextView)findViewById(R.id.valtestdate);
		status = (TextView)findViewById(R.id.valstatus);
		content = (TextView)findViewById(R.id.valContent);
		
		buttonComment = (Button)findViewById(R.id.idbttcomment);
		buttonFixComplete = (Button)findViewById(R.id.idbttonFixcomplete);
		
		lstViewComment = (ListView)findViewById(R.id.listViewComment);
		
		
		lbTitle = (TextView)findViewById(R.id.idLabelTitle);
		lbTester = (TextView)findViewById(R.id.idtester);
		lbTestDate = (TextView)findViewById(R.id.idtestdate);
		lbStatus = (TextView)findViewById(R.id.idstatus);
		lbContent = (TextView)findViewById(R.id.idcontent);
		listComent = (TextView)findViewById(R.id.listComment);
		
		lbTitle.setText(language.getString("title") + ":");
		lbTester.setText(language.getString("tester")+ ":");
		lbTestDate.setText(language.getString("test_date")+ ":");
		lbStatus.setText(language.getString("ac_Status")+ ":");
		lbContent.setText(language.getString("content")+ ":");
		listComent.setText(language.getString("lst_comment"));
		buttonComment.setText(language.getString("comment"));
		buttonFixComplete.setText(language.getString("status_error1"));
		
		Intent intent = this.getIntent();
		prProject = (PrProject)intent.getSerializableExtra("prproject");
		emp =(Employee)intent.getSerializableExtra("emp");
		prTestProject = (PrTestProject)intent.getSerializableExtra("prprojecttest");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

		
		curNameProject.setText(prProject.getNameProject());
//		title.setText(prTestProject.getTitle());
		title.setText(prTestProject.getTitleview());
		tester.setText(prTestProject.getEmployeeName());
		testDate.setText(prTestProject.getTime() + "");
		if(prTestProject.getStatus() == 0){
//			status.setText("Not fix");
			status.setText(language.getString("status_error2"));
		}else if(prTestProject.getStatus() == 1){
//			status.setText("Fix complete");
			status.setText(language.getString("status_error1"));
		}else{
//			status.setText("Delete");
			status.setText(language.getString("Delete"));
		}
		content.setText(prTestProject.getText());
		
		if(prTestProject.getStatus() == 1){
//			buttonComment.setClickable(false);
//			buttonFixComplete.setClickable(false);
			buttonComment.setEnabled(false);
			buttonFixComplete.setEnabled(false);
			try {
				ProjectCommentErrorAdapter prAdapter = new ProjectCommentErrorAdapter(this, getAllCommentByIdTest());
				lstViewComment.setAdapter(prAdapter);
//				lstViewComment.setAdapter(new ArrayAdapter<PrComment>(this, android.R.layout.simple_list_item_1,getAllCommentByIdTest()));
//				lstViewComment.setOnItemClickListener(showAlertDelete);
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else{
		buttonComment.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				showPopupComment();
			}
		});
//		buttonFixComplete.setOnClickListener(processButtonFixComplete);
		buttonFixComplete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showPopupFixComplete();
			}
		});
		
		try {
			ProjectCommentErrorAdapter prAdapter = new ProjectCommentErrorAdapter(this, getAllCommentByIdTest());
			lstViewComment.setAdapter(prAdapter);
//			lstViewComment.setAdapter(new ArrayAdapter<PrComment>(this, android.R.layout.simple_list_item_1,getAllCommentByIdTest()));
			lstViewComment.setOnItemClickListener(showAlertDelete);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
	}
private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			Intent i=new Intent(ProcessButtonViewErrorManagerProject.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	private void showPopupComment() {
		AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(language.getString("addcomment"));
		 final EditText editText = new EditText(ProcessButtonViewErrorManagerProject.this);
		 helpBuilder.setIcon(R.drawable.icon);
//		 final CharSequence[] items = {"No fix","Error","Fix complete"};
		 final CharSequence[] items = {language.getString("status_error2"),language.getString("error"),language.getString("fixcomplete")};
		 helpBuilder.setIcon(R.drawable.icon);
		 helpBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int item) {
				if(items[item]==language.getString("status_error2")){
					namecolor="#B023AB";
					Spannable WordtoSpan = new SpannableString(editText.getText().toString());        
					WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, editText.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.setText(WordtoSpan);
				}else if(items[item]==language.getString("error")){
					namecolor="#B02323";
					Spannable WordtoSpan = new SpannableString(editText.getText().toString());        
					WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, editText.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.setText(WordtoSpan);
				}else if(items[item]==language.getString("statusfixcopmp")){
					namecolor="#3DA850";
					Spannable WordtoSpan = new SpannableString(editText.getText().toString());        
					WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor(namecolor)), 0, editText.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.setText(WordtoSpan);
				}
				
			}
		});
		 
		 
		 
		 helpBuilder.setView(editText);
		 helpBuilder.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	CommentDAO.addNewCommentErrorDAO_tc(getDatabase(), prTestProject.getIdTest(), emp.getIdemployee(), editText.getText().toString(),namecolor);
		    	
		    		Intent intent = new Intent(ProcessButtonViewErrorManagerProject.this,ProcessButtonViewErrorManagerProject.class);
		    		intent.putExtras(getIntent().getExtras());
		    		intent.putExtra("prproject", prProject);
					intent.putExtra("emp", emp);
					intent.putExtra("prprojecttest", prTestProject);
					intent.putExtra("database", getDatabase());
					intent.putExtra("lang", lang);
					startActivity(intent);
		    }
		   });

		 helpBuilder.setNegativeButton(language.getString("ac_Cancel"), new DialogInterface.OnClickListener() {

		
		  public void onClick(DialogInterface dialog, int which) {
		   // Do nothing
		  }
		 });
		 

		 // Remember, create doesn't show the dialog
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();

	}
	
	private void showPopupFixComplete(){
		AlertDialog.Builder popupFixComp = new AlertDialog.Builder(this);
		popupFixComp.setTitle(language.getString("fixcomplete"));
		popupFixComp.setMessage(language.getString("questionFixComp"));
		
		popupFixComp.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				TestProjectDAO.updateTestStatus(prTestProject.getIdTest(), getDatabase(),prTestProject.getTitleview());
				Intent intent = new Intent(ProcessButtonViewErrorManagerProject.this, ListErrorProjectManager.class);
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("prproject", prProject);
				intent.putExtra("empl", emp);
				intent.putExtra("database", getDatabase());
				intent.putExtra("lang", lang);
				
				startActivity(intent);
				
			}
		});
		
		popupFixComp.setNegativeButton(language.getString("ac_Cancel"), new DialogInterface.OnClickListener() {

			
			  public void onClick(DialogInterface dialog, int which) {
			   // Do nothing
			  }
			 });
			 

			 // Remember, create doesn't show the dialog
			 AlertDialog helpDialog = popupFixComp.create();
			 helpDialog.show();
	}
	

	
//	private OnClickListener processButtonFixComplete = new OnClickListener() {
//		
//		public void onClick(View v) {
//				TestProjectDAO.updateTestStatus(prTestProject.getIdTest(), getDatabase());
//				Intent intent = new Intent(ProcessButtonViewErrorManagerProject.this, ListErrorProjectManager.class);
//				intent.putExtras(getIntent().getExtras());
//				intent.putExtra("prproject", prProject);
//				intent.putExtra("empl", emp);
//				intent.putExtra("database", getDatabase());
//				intent.putExtra("lang", lang);
//				
//				startActivity(intent);
//			
//		}
//	};
	
	@SuppressWarnings("unused")
	private void reloadThisPage() throws SQLException{
		ProjectCommentErrorAdapter prAdapter = new ProjectCommentErrorAdapter(this, getAllCommentByIdTest());
		lstViewComment.setAdapter(prAdapter);
//		lstViewComment.setAdapter(new ArrayAdapter<PrComment>(this, android.R.layout.simple_list_item_1,getAllCommentByIdTest()));
	}

	private ArrayList<PrComment> getAllCommentByIdTest() throws SQLException{
		ArrayList<PrComment> arrComment = new ArrayList<PrComment>();
		arrComment = CommentDAO.getCommentByIDTestProject(prTestProject.getIdTest(), getDatabase());
		return arrComment;
	}
	
	private OnItemClickListener showAlertDelete = new OnItemClickListener() {

		
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			AlertDialog.Builder adb=new AlertDialog.Builder(ProcessButtonViewErrorManagerProject.this);
			
			 prcom=(PrComment) lstViewComment.getItemAtPosition(arg2);
			
			adb.setTitle(language.getString("Delete"));
				adb.setMessage(language.getString("questiondelete"));
				adb.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						try {
							deleteItemSelected(prcom.getIdComment());
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}});
				adb.setNegativeButton(language.getString("ac_Cancel"), null);
			adb.show();
		}
	};
	private void deleteItemSelected(String comment) throws SQLException{
		CommentDAO.deleteItemComment(getDatabase(),comment);
		ProjectCommentErrorAdapter adapter1 = new ProjectCommentErrorAdapter(this, getAllCommentByIdTest());
		lstViewComment.setAdapter(adapter1);
	}
}
